import { Component, Input, OnInit ,ViewChild} from '@angular/core';
import { SFSchema,SFSliderWidgetSchema } from '@delon/form';
import { ApiService } from '@core/services/api';
import { HttpClient } from '@angular/common/http';
import { NzMessageService, NzModalRef, UploadFile } from 'ng-zorro-antd';
import { FormBuilder, FormGroup } from '@angular/forms';
import { UtilService } from '@core/services/util.service';
import { NzTabPosition } from 'ng-zorro-antd/tabs';


function getBase64(file: File): Promise<string | ArrayBuffer | null> {
  return new Promise((resolve, reject) => {
    const reader = new FileReader();
    reader.readAsDataURL(file);
    reader.onload = () => resolve(reader.result);
    reader.onerror = error => reject(error);
  });
}

@Component({
  selector: 'app-device-set-batch',
  templateUrl: './device-set-batch.component.html',
  styles: []
})
export class DeviceSetBatchComponent implements OnInit {
  nzTabPosition: NzTabPosition = 'left';
  @ViewChild('formBase',{ static: false }) formBase:any;
  @ViewChild('formVersion',{ static: false }) formVersion:any;
  @ViewChild('formSet',{ static: false }) formSet:any;
  @ViewChild('tree',{static:false}) tree: any;
  @Input()
  set _param(value) {
    if (!value) return;
    this.param = value;
  };
  param: any;
  param2:any;
  param3:any;
  param4 : any;
  houseList = [];
  searchValue:any="";
  isLoading:boolean=true;
  defaultCheckedKeys:any=[];
  showForm = false;

  
  schema2: SFSchema = {
    properties: {
      deviceOpenFace: {
        type: 'boolean',
        title: '人脸开门',
        ui: {
          checkedChildren: '开',
          unCheckedChildren: '关',
          grid: {span: 8}
        }
      },
      deviceBioAssay: {
        type: 'boolean',
        title: '活体检测',
        ui: {
          checkedChildren: '开',
          unCheckedChildren: '关',
          grid: {span: 8}
        }
      },
      deviceFaceSimilarity: {
        type:'number',
        title:'人脸比对相似阈值',
        minimum: 0,
        maximum: 100,
        ui:{
          grid: {span: 8}
        }
      },
      deviceBioAssaySimilarity: {
        type:'number',
        title:'活体检测阈值',
        minimum: 0,
        maximum: 100,
        ui:{
          grid: {span: 8}
        }
      },
      deviceFaceStorage: {
        type:'string',
        title:'人脸库存储上限',
        // minimum: 0,
        // maximum: 1024,
        readOnly:true,
        ui:{
          grid: {span: 8}
        }
      },
  
    },
    "ui": {
      "spanLabelFixed": 150,
      "grid": {
        "span": 24
      }
    },
    required: []
  };
  schema3: SFSchema = {
    properties: {
      deviceIntercom: {
        type: 'boolean',
        title: '对讲功能',
        ui: {
          checkedChildren: '开',
          unCheckedChildren: '关',
          grid: {span: 24}
        }
      },
      deviceVolumeCall: {
        type:'number',
        title:'门禁通话音量',
        minimum: 0,
        maximum: 100,
        ui: {
          widget:'slider',
          grid: {span: 12}
        } as SFSliderWidgetSchema
      },
      deviceVolumeAdvertising: {
        type:'number',
        title:'门禁广告音量',
        minimum: 0,
        maximum: 100,
        ui: {
          widget:'slider',
          grid: {span: 12}
        } as SFSliderWidgetSchema
      },
      deviceVolumeMic: {
        type:'number',
        title:'门禁mic增益',
        minimum: 0,
        maximum: 100,
        ui: {
          widget:'slider',
          grid: {span: 12}
        } as SFSliderWidgetSchema
      },
      deviceVolumeBell: {
        type:'number',
        title:'门禁铃声音量',
        minimum: 0,
        maximum: 100,
        ui: {
          widget:'slider',
          grid: {span: 12}
        } as SFSliderWidgetSchema
      },
      deviceVolumeHost: {
        type:'number',
        title:'门禁提示音量',
        minimum: 0,
        maximum: 100,
        ui: {
          widget:'slider',
          grid: {span: 12}
        } as SFSliderWidgetSchema
      },
    },
    "ui": {
      "spanLabelFixed": 150,
      "grid": {
        "span": 24
      }
    },
    required: []
  };
  schema4: SFSchema = {
    properties: {
      deviceId: {
        type: 'string',
        ui:{
          hidden:true
        }
      },
      deviceFacePat: {
        type: 'boolean',
        title: '人脸抓拍',
        ui: {
          checkedChildren: '开',
          unCheckedChildren: '关',
          grid: {span: 8}
        }
      },
      deviceFacePatVideoUpload: {
        type: 'boolean',
        title: '人脸抓拍视频上报',
        ui: {
          checkedChildren: '开',
          unCheckedChildren: '关',
          grid: {span :8}
        }
      },
      deviceShutScreen:{
        type:'boolean',
        title:'门禁息屏状态',
        ui: {
          checkedChildren: '开',
          unCheckedChildren: '关',
          grid:{span: 8}
        }
      },
      deviceAdvertising: {
        type: 'boolean',
        title: '门禁广告',
        ui: {
          checkedChildren: '开',
          unCheckedChildren: '关',
          grid: {span: 8}
        }
      },
      deviceAccessRecord: {
        type: 'boolean',
        title: '门禁出入记录上报',
        ui: {
          checkedChildren: '开',
          unCheckedChildren: '关',
          grid: {span :8}
        }
      },
      deviceAccessRecordPicture: {
        type: 'boolean',
        title: '门禁出入记录图片上报',
        ui: {
          checkedChildren: '开',
          unCheckedChildren: '关',
          grid: {span :8}
        }
      },
      deviceAccessRecordVideo: {
        type: 'boolean',
        title: '门禁出入记录视频上报',
        ui: {
          checkedChildren: '开',
          unCheckedChildren: '关',
          grid: {span :8}
        }
      },
      deviceLed: {
        type: 'boolean',
        title: '门禁LED灯状态',
        ui: {
          checkedChildren: '开',
          unCheckedChildren: '关',
          grid: {span :8}
        }
      },
      deviceAndroidPassword: {
        type:'string',
        title:'系统设置密码',
        ui: {
          type:'password',
          grid: {span: 8}
        }
      },
      deviceTamperAlarm: {
        type: 'boolean',
        title: '防拆报警',
        ui: {
          checkedChildren: '开',
          unCheckedChildren: '关',
          grid: {span :8}
        }
      },
      deviceDoorOpenAlarm: {
        type: 'boolean',
        title: '长开门报警',
        ui: {
          checkedChildren: '开',
          unCheckedChildren: '关',
          grid: {span :8}
        }
      },
      doorOpenTime: {
        type:'number',
        title:'开门时间',
        minimum: 5,
        maximum: 20,
        ui:{
          grid: {span: 8}
        }
      },
      deviceRemark: {
        type:'string',
        title:'备注'
      }
    },
    "ui": {
      "spanLabelFixed": 150,
      "grid": {
        "span": 24
      }
    },
    required: ["deviceId"]
  }


  submitBase(data){
    this.submitData();
  }
  submitVersion(data){
    this.submitData();
  }
  formSubmit(data) {
    this.submitData();
  }

  submitData(){
    let baseData=this.formBase.value;
    let versionData=this.formVersion.value;
    let setData=this.formSet.value;
    baseData.deviceOpenFace = baseData.deviceOpenFace == true ? 1 : 0 ;
    baseData.deviceBioAssay = baseData.deviceBioAssay == true ? 1 : 0 ;
    versionData.deviceIntercom = versionData.deviceIntercom == true ? 1 : 0 ;
    setData.deviceFacePat = setData.deviceFacePat == true ? 1 : 0 ;
    setData.deviceFacePatVideoUpload = setData.deviceFacePatVideoUpload == true ? 1 : 0;
    setData.deviceShutScreen = setData.deviceShutScreen == true ? 1 : 0 ;
    setData.deviceAdvertising = setData.deviceAdvertising == true ? 1 : 0 ;
    setData.deviceAccessRecord = setData.deviceAccessRecord == true ? 1 : 0 ;
    setData.deviceAccessRecordPicture = setData.deviceAccessRecordPicture == true ? 1 : 0 ;
    setData.deviceAccessRecordVideo = setData.deviceAccessRecordVideo == true ? 1 : 0 ;
    setData.deviceLed = setData.deviceLed == true ? 1 : 0 ;
    setData.deviceTamperAlarm = setData.deviceTamperAlarm == true ? 1 : 0;
    setData.deviceDoorOpenAlarm = setData.deviceDoorOpenAlarm == true ? 1 : 0
    let body={...baseData,...versionData,...setData};
    this.param.forEach(element => {
      
    });
    console.log(body);
    this.http.request<any>("post", "/core/host/rke/update",  { body}).subscribe(res => {
      console.log(res)
      if (res.status === '200') {
        this.msg.success('操作成功');
        //this.modalRef.destroy('ok');
      } else {
        this.msg.error(res.message)
      }
    })


  }

  getDeviceInfo(deviceId: string): Promise<any>{
    let body = {id:deviceId};
    return this.http.request<any>('POST', '/core/host/details',{body}).toPromise();
  }

  constructor(
    private api: ApiService,
    private http: HttpClient,
    private msg: NzMessageService,
    private util:UtilService,
    private modalRef: NzModalRef,
    private fb:FormBuilder
  ) { }

  async ngOnInit() {
    this.showForm = true;
    // this.validateForm = this.fb.group({
    //   btime:new Date(),
    //   etime:new Date(),
    //   cbtime:new Date(),
    //   cetime:new Date(),
    //   lbtime:new Date(),
    //   letime:new Date(),
    //   // rangePickerTime: [[new Date(this.param.cardUsingStartTime),new Date(this.param.cardUsingEndTime)]]
    // });
  }
    resetForm(data){
      this.modalRef.destroy("ok");
     }


}
